﻿using Newtonsoft.Json;
using SpiderStudy.Utils.Currency;
using SpiderStudy.Utils.Html;
using System;
using System.Collections.Generic;
using System.Threading;

namespace SpiderStudy.WebSite.Douban
{
    internal class CreateContent
    {
        public string InitContent(string ComicName)
        {
            Sleep();
            string MovieId = SearchMoive(ComicName);
            if (MovieId == "")
            {
                Console.WriteLine("未找到结果！或连续请求被限制！");
                return "";
            }

            string Content = GetMovieInfo(MovieId);
            Content += GetMovieComment(MovieId);

            return Content;
        }

        private static void Sleep()
        {
            int SleepTime = new Random().Next(20000, 60000);
            Console.WriteLine("防止豆瓣封禁，休眠{0}秒", SleepTime / 1000);
            Thread.Sleep(SleepTime);
        }

        /// <summary>
        /// 获取电影评论
        /// </summary>
        /// <param name="moiveId"></param>
        /// <returns></returns>
        private string GetMovieComment(string MovieId)
        {
            string Comment = "";
            string API = string.Format(new DoubanStaticValue().CommentAPI1, MovieId);
            string Shtml = WebHelper.HttpGet(API);

            List<string> CommentList = HtmlHelper.GetHtmlValueList(Shtml, "//div[@class='comment-item']");

            while (true)
            {
                if (CommentList.Count == 0)
                    break;

                int Random = new Random().Next(CommentList.Count);
                Console.WriteLine(Random + "--" + CommentList.Count);
                String Html = CommentList[Random];
                CommentList.RemoveAt(Random);
                string GetTextComment = HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(Html, "//div[@class='comment']/p"));

                Console.WriteLine(GetTextComment.Length + "文本长度" + GetTextComment.Length);
                if (GetTextComment.Length < 20 && CommentList.Count > 1)
                    continue;
                Comment = "\r\n" + HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(Html, "//span[@class='comment-info']/a")) + ":" +  //获取用户昵称
                    HtmlHelper.GetFirstValue(HtmlHelper.GetAttributesVlaueList(Html, "title", "//span[@class='comment-info']/span[2]")) + "\r\n" +
                    GetTextComment;
                break;
            }
            return Comment;
        }

        /// <summary>
        /// 获取电影介绍
        /// </summary>
        /// <returns></returns>
        private string GetMovieInfo(string MoiveId)
        {
            string Shtml = WebHelper.HttpGet(string.Format(new DoubanStaticValue().MovieInfoAPI1, MoiveId));

            try
            {
                ArticleInfo.Root ArticleInfo = JsonConvert.DeserializeObject<ArticleInfo.Root>(Shtml);
                return string.Format("{0}简介：\r\n{1}", ArticleInfo.title, ArticleInfo.summary);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                Console.WriteLine("电影信息获取失败");
            }
            return "";
        }

        /// <summary>
        /// 搜索电影，返回电影的豆瓣ID
        /// </summary>
        /// <param name="comicName"></param>
        /// <returns></returns>
        private string SearchMoive(string ComicName)
        {
            String API = string.Format(new DoubanStaticValue().SearchApi1, new TextHelper().UrlEncodeToUtf8(ComicName));
            try
            {
                string Shtml = WebHelper.HttpGet(API);
                Console.WriteLine(Shtml);
                SearchInfo.Root SearchJosn = JsonConvert.DeserializeObject<SearchInfo.Root>(Shtml);
                return SearchJosn.subjects[0].id;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                Console.WriteLine("无搜索结果");
            }
            return "";
        }
    }
}